package tpenchere.ui.contents;

import java.util.Date;
import java.util.List;

import javax.swing.JMenuBar;
import javax.swing.JToolBar;

import org.jdesktop.application.ResourceMap;
import org.jdesktop.swingx.JXDatePicker;

import tpenchere.application.MainApplication;
import tpenchere.controllers.BidController;
import tpenchere.controllers.ControllerInterface;
import tpenchere.ui.AbstractView;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFormattedTextField;
import java.awt.ComponentOrientation;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import java.awt.Dimension;

public class BidWindow extends AbstractView {
	private final ResourceMap resourceMap = MainApplication.getApplication().getContext().getResourceMap(this.getClass());
	private BidController controller;
	private JTextField productLabel;
	private JTextField productDescription;
	private JFormattedTextField productStartingBid;
	private JXDatePicker productDueDate;
	private JComboBox productCategory;
	
	public BidWindow() {
		controller = new BidController(this);
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblProductLabel = new JLabel(resourceMap.getString("view.bid.product.label"));
		add(lblProductLabel, "2, 4, right, default");
		
		productLabel = new JTextField();
		add(productLabel, "4, 4, left, default");
		productLabel.setColumns(10);
		
		JLabel lblCategory = new JLabel("Category");
		add(lblCategory, "2, 6, right, default");
		
		productCategory = new JComboBox();
		productCategory.setEditable(true);
		add(productCategory, "4, 6, left, default");
		
		JLabel lblDescription = new JLabel(resourceMap.getString("view.bid.product.description"));
		add(lblDescription, "2, 8, right, default");
		
		productDescription = new JTextField();
		add(productDescription, "4, 8, fill, default");
		productDescription.setColumns(10);
		
		JLabel lblStartingBid = new JLabel(resourceMap.getString("view.bid.product.startingBid"));
		add(lblStartingBid, "2, 10, right, default");
		
		productStartingBid = new JFormattedTextField(new Float(0.00f));
		productStartingBid.setPreferredSize(new Dimension(80, 20));
		productStartingBid.setMaximumSize(new Dimension(80, 20));
		productStartingBid.setMinimumSize(new Dimension(80, 20));
		productStartingBid.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		add(productStartingBid, "4, 10, left, default");
		
		JLabel lblDueDate = new JLabel(resourceMap.getString("view.bid.product.dueDate"));
		add(lblDueDate, "2, 12, right, default");
		
		//productDueDate = new JFormattedTextField();
		productDueDate = new JXDatePicker();
		add(productDueDate, "4, 12, left, default");
		
		JPanel panel = new JPanel();
		add(panel, "4, 14, fill, fill");
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JButton btnCreate = new JButton(controller.getActionMap().get("view.bid.button.create"));
		panel.add(btnCreate, "2, 2");
		
		JButton btnCancel = new JButton(controller.getActionMap().get("view.bid.button.cancel"));
		panel.add(btnCancel, "4, 2");
		
		controller.loadCategories();
	}

	@Override
	public ControllerInterface getController() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public JMenuBar getMenuBar() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<JToolBar> getToolbars() {
		// TODO Auto-generated method stub
		return null;
	}

	public JTextField getProductLabel() {
		return productLabel;
	}

	public void setProductLabel(JTextField productLabel) {
		this.productLabel = productLabel;
	}

	public JTextField getProductDescription() {
		return productDescription;
	}

	public void setProductDescription(JTextField productDescription) {
		this.productDescription = productDescription;
	}

	public JFormattedTextField getProductStartingBid() {
		return productStartingBid;
	}

	public void setProductStartingBid(JFormattedTextField productStartingBid) {
		this.productStartingBid = productStartingBid;
	}

	public JXDatePicker getProductDueDate() {
		return productDueDate;
	}

	public void setProductDueDate(JXDatePicker productDueDate) {
		this.productDueDate = productDueDate;
	}

	public JComboBox getProductCategory() {
		return productCategory;
	}

	public void setProductCategory(JComboBox productCategory) {
		this.productCategory = productCategory;
	}
	
	public void updateView() {
		productCategory.setSelectedItem(null);
		productDueDate.setDate(new Date());
		productDescription.setText("");
		productLabel.setText("");
		productStartingBid.setValue(0.00f);
		controller.loadCategories();
	}
}
